﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace MvvmFx.Common.ExtensionMethods
{
       /// <summary>
    /// Static utility class containing extensions for lists.
    /// </summary>
    public static class ListExtensionMethods
    {
        /// <summary>
        /// Returns a subset of the specified List<T> using the specified Predicate<T>
        /// </summary>
        /// <typeparam name="T">The type of item in the list</typeparam>
        /// <param name="array">The list of items</param>
        /// <param name="match">The predicate to use to find the matching sublist</param>
        /// <returns>The sublist of items that match the predicate</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists")]
        public static List<T> FindAll<T>(this List<T> array, Predicate<T> match)
        {
            if (array == null)
            {
                throw new ArgumentException("array");
            }

            return (from item in array where match(item) select item).ToList();
        }

        /// <summary>
        /// Returns whether the list contains any of the specified items
        /// </summary>
        /// <param name="source">The list to check in</param>
        /// <param name="items">The items to check for</param>
        /// <returns>true if the source list contains at least one item from the items, false otherwise</returns>
        public static bool ContainsAny(this List<string> source, List<string> items)
        {
            foreach (var item in source)
            {
                if (items.Contains(item))
                    return true;
            }

            return false;
        }
    }
}